<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="上涨" prop="up">
      <el-input v-model="dataForm.up" placeholder="上涨"></el-input>
    </el-form-item>
    <el-form-item label="下跌" prop="down">
      <el-input v-model="dataForm.down" placeholder="下跌"></el-input>
    </el-form-item>
    <el-form-item label="涨停" prop="limitUpNumber">
      <el-input v-model="dataForm.limitUpNumber" placeholder="涨停"></el-input>
    </el-form-item>
    <el-form-item label="跌停" prop="limitDownNumber">
      <el-input v-model="dataForm.limitDownNumber" placeholder="跌停"></el-input>
    </el-form-item>
    <el-form-item label="炸板" prop="breakLimitUpNumber">
      <el-input v-model="dataForm.breakLimitUpNumber" placeholder="炸板"></el-input>
    </el-form-item>
    <el-form-item label="" prop="date">
      <el-input v-model="dataForm.date" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          up: '',
          down: '',
          limitUpNumber: '',
          limitDownNumber: '',
          breakLimitUpNumber: '',
          date: ''
        },
        dataRule: {
          up: [
            { required: true, message: '上涨不能为空', trigger: 'blur' }
          ],
          down: [
            { required: true, message: '下跌不能为空', trigger: 'blur' }
          ],
          limitUpNumber: [
            { required: true, message: '涨停不能为空', trigger: 'blur' }
          ],
          limitDownNumber: [
            { required: true, message: '跌停不能为空', trigger: 'blur' }
          ],
          breakLimitUpNumber: [
            { required: true, message: '炸板不能为空', trigger: 'blur' }
          ],
          date: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/stockstatistics/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.up = data.stockStatistics.up
                this.dataForm.down = data.stockStatistics.down
                this.dataForm.limitUpNumber = data.stockStatistics.limitUpNumber
                this.dataForm.limitDownNumber = data.stockStatistics.limitDownNumber
                this.dataForm.breakLimitUpNumber = data.stockStatistics.breakLimitUpNumber
                this.dataForm.date = data.stockStatistics.date
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/stockstatistics/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'up': this.dataForm.up,
                'down': this.dataForm.down,
                'limitUpNumber': this.dataForm.limitUpNumber,
                'limitDownNumber': this.dataForm.limitDownNumber,
                'breakLimitUpNumber': this.dataForm.breakLimitUpNumber,
                'date': this.dataForm.date
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
